# -*- coding: utf-8 -*-
"""
Created on Thu Sep  2 22:43:07 2021

@author: xiong
"""
import arcpy
import arcpy.mapping as mp
import xlrd
import xlwt
import sys
import os
import shutil
import traceback
import csv
import time
from shutil import copyfile
#import pandas as pd

reload(sys)
sys.setdefaultencoding('utf-8')
############这都要改
Naame  =    r'973_刺芒野古草草丛'
FIDList  = [109,125,135,238,254,263,303,339,391,414,468,510,643,816,1042,1126,1199,9,265,361,422,446,525,917,1037,1111,1174,1223] #重新算的图斑的FID ，可以一个一个写[1,345,555,54] 或者像这样写范围，这个范围是左闭右开的
workSpace = r'E:\tutu\123\基础'
dataFrameName = '图层' #5大图数据框名字
############下面这些只第一次改



tubanNaame = Naame + '_图斑'
tubanPath = workSpace + "\\" +tubanNaame + '.shp'   

rowList = []
with arcpy.da.UpdateCursor(tubanPath,('FID',"Shape@",'type','name','value','real_TB','rate','belong_TB','区域','小区域','类型')) as cursor:
    for row in cursor:
        rowList.append(row)
        cursor.deleteRow()
        
for t in range(0,len(rowList)):
    if rowList[t][-3] in FIDList:
        with arcpy.da.InsertCursor(tubanPath,('FID',"Shape@",'type','name','value','real_TB','rate','belong_TB','区域','小区域','类型'))as insyoubiao:
            insyoubiao.insertRow(rowList[t])
            
        arcpy.SelectLayerByAttribute_management(Naame,"NEW_SELECTION",'"tb"> 0')
        arcpy.SelectLayerByLocation_management(Naame,"COMPLETELY_WITHIN",tubanPath)
        countList = [0,0,0,0,0,0,0,0]
        with arcpy.da.UpdateCursor(Naame,('tbclass'))as coursor:
            for row in coursor:
                countList[int(row[0])] += 1
        str1 = ''
        str2 = ''
        swic = 0
        for j in range(8):
            if countList[j]:
                str1 += str(j)
                kk = round(float(countList[j])/sum(countList),5)
                str2 += ' ' + str(kk)
                if kk > 0.6324:
                    swic  = j
        if str1 == '':
            rowList[t][-6] = 999
        else:
            rowList[t][-6] = int(str1)
        rowList[t][-5] = str2
        rowList[t][-4] = swic 
        with arcpy.da.UpdateCursor(tubanPath,('FID',"Shape@",'type','name','value','real_TB','rate','belong_TB','区域','小区域','类型'))as coursor:
            for row in coursor:
                coursor.deleteRow()
            print('删除行结束')
            
with arcpy.da.InsertCursor(tubanPath,('FID',"Shape@",'type','name','value','real_TB','rate','belong_TB','区域','小区域','类型'))as insyoubiao:
    for i in range(len(rowList)):  
           insyoubiao.insertRow(rowList[i])
